package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sq.universe.base.controller.BaseController;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.system.entity.UserRoleEntity;
import com.sq.universe.system.service.UserRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/userRole")
@CrossOrigin
public class UserRoleController extends BaseController {

    @Autowired
    private UserRoleService userRoleService;

    /**
    * 查询数据
    * @param userRoleEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:userRole:query")
    public RespEntity query(@RequestBody(required = false) UserRoleEntity userRoleEntity){
        try {
            return new RespEntity("查询成功",userRoleService.query(userRoleEntity));
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param userRoleEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:userRole:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) UserRoleEntity userRoleEntity){
        try {
            PageHelper.startPage(userRoleEntity.getPageNo(),userRoleEntity.getPageSize());
            List<UserRoleEntity> userRoleList = userRoleService.query(userRoleEntity);
            PageInfo<UserRoleEntity> pageInfo = new PageInfo<UserRoleEntity>(userRoleList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param userRoleEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:userRole:insert")
    public RespEntity insert(@RequestBody(required = false) UserRoleEntity userRoleEntity){
        try {
            userRoleEntity.setUrCreateTime(new Date());
            userRoleEntity.setUrCreateUser(this.getCurrentUser().getUserId());
            userRoleService.insert(userRoleEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param userRoleEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:userRole:update")
    public RespEntity update(@RequestBody(required = false) UserRoleEntity userRoleEntity){
        try {
            userRoleEntity.setUrUpdateTime(new Date());
            userRoleEntity.setUrUpdateUser(this.getCurrentUser().getUserId());
            userRoleService.update(userRoleEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param userRoleEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:userRole:delete")
    public RespEntity delete(@RequestBody(required = false) UserRoleEntity userRoleEntity){
        try {
            userRoleEntity.setUrUpdateTime(new Date());
            userRoleEntity.setUrUpdateUser(this.getCurrentUser().getUserId());
            userRoleService.logicalDelete(userRoleEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            exception.printStackTrace();
            return new RespEntity("删除失败");
        }
    }
}